﻿using System;
using System.Web.Mvc;
using Framework.Domain;
using Shell.DomainDot;
using Framework.Mvc.ViewModels;
using Framework.Poco;
namespace Framework.Mvc.Controllers
{
    public class ArticleController : Controller
    {
        private IDomainDotFactory factory;

        public ArticleController()
        {
            this.factory = DomainDotFactory.Factory;
        }

        //
        // GET: /Article/List
        public ActionResult List(User currentUser, Guid? categoryId, int currentPage = 1)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                if (categoryId.HasValue)
                {
                    var domain = this.factory.Create<SelectArticleByCategory>(categoryId);
                    var categoryContext = factory.Create<SelectCategory>();
                    var total = 0;
                    var articles = domain.Select(currentPage, Constans.PageSize, out total);
                    var model = new ArticleListViewModel();
                    model.Category = categoryContext.Select(categoryId);
                    model.CurrentPage = currentPage;
                    model.PageSize = Constans.PageSize;
                    model.Total = total;
                    model.List = articles;
                    return View(model);
                }
                else
                {
                    return this.RedirectToAction("ArticleList", "Admin");
                }
            }
            return this.RedirectToAction("Index", "Home");
        }

        //
        // GET: /Article/Create
        public ActionResult Create()
        {
            var model = new ArticleViewModel();
            model.CategoryId = Guid.Empty;
            var domain = this.factory.Create<SelectCategory>();
            model.Categories = domain.GetAllCategories();
            return View(model);
        }

        //
        // POST: /Article/Create
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(ArticleViewModel model)
        {
            try
            {
                // TODO: Add insert logic here
                if (!Valid(this.ModelState, model.Instance))
                {
                    return View(model);
                }
                var domain = this.factory.Create<CreateArticle>(model.CategoryId);
                domain.Create(model.Instance);
                return RedirectToAction("List", new { categoryId = model.CategoryId });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Article/Edit/5
        public ActionResult Edit(Guid categoryId, Guid articleId)
        {
            var domain = this.factory.Create<SelectArticle>(categoryId);
            var article = domain.Select(articleId);
            if (article == null)
            {
                return RedirectToAction("List", new { categoryId = categoryId });
            }
            var categoryDomain = this.factory.Create<SelectCategory>();
            var model = new ArticleViewModel();
            model.CategoryId = categoryId;
            model.Category = categoryDomain.Select(categoryId);
            model.Categories = categoryDomain.GetAllCategories();
            model.Instance = article;
            return View(model);
        }

        //
        // POST: /Article/Edit/5
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(ArticleViewModel model)
        {
            try
            {
                // TODO: Add update logic here
                if (!Valid(this.ModelState, model.Instance))
                {
                    return View(model);
                }
                var domain = this.factory.Create<UpdateArticle>(model.CategoryId);
                domain.Update(model.Instance);
                return RedirectToAction("List", new { categoryId = model.CategoryId });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Article/Delete/5

        public ActionResult Delete(Guid categoryId, Guid articleId)
        {

            var domain = this.factory.Create<SelectArticle>(categoryId);
            var article = domain.Select(articleId);
            if (article == null)
            {
                return RedirectToAction("List", new { categoryId = categoryId });
            }
            var model = new ArticleViewModel();
            model.CategoryId = categoryId;
            model.Categories = this.factory.Create<SelectCategory>().GetAllCategories();
            model.Instance = article;
            return View(model);
        }

        //
        // POST: /Article/Delete/5

        [HttpPost]
        public ActionResult Delete(ArticleViewModel model)
        {
            try
            {
                // TODO: Add delete logic here
                var domain = DomainDotFactory.Factory.Create<DeleteArticle>(model.CategoryId);
                domain.Delete(model.Instance.Id);

                return RedirectToAction("List", new { categoryId = model.CategoryId });
            }
            catch
            {
                return View();
            }
        }

        private bool Valid(ModelStateDictionary state, Article instance)
        {
            bool isValid = true;
            if (instance == null)
            {
                state.AddModelError("Instance", "实例为null。");
                isValid = false;
            }
            if (string.IsNullOrWhiteSpace(instance.Name))
            {
                state.AddModelError("Name", "标题为空。");
                isValid = false;
            }
            if (string.IsNullOrWhiteSpace(instance.Content))
            {
                state.AddModelError("Content", "内容为空。");
                isValid = false;
            }
            return isValid;
        }

    }
}
